//
// Created by LEI XU on 4/27/19.
//
#include <eigen3/Eigen/Eigen>
#include "Texture.hpp"

Eigen::Vector3f Texture::biInterpolate(float u, float v){
    auto u_img = u * width;
    auto v_img = (1 - v) * height;

    auto x0 = (int)u_img;
    auto x1 = x0 + 1;

    auto y0 = (int)v_img;
    auto y1 = y0 + 1;

    auto s = u_img - (float)x0;
    auto t = v_img - (float)y0;


    auto color1 = image_data.at<cv::Vec3b>(y0, x0);
    auto color2 = image_data.at<cv::Vec3b>(y0, x1);
    auto color3 = image_data.at<cv::Vec3b>(y1, x0);
    auto color4 = image_data.at<cv::Vec3b>(y1, x1);

    auto color_v1 = color1 + s * (color2 - color1);
    auto color_v2 = color3 + s * (color4 - color3);
    auto final_color = color_v1 + t * (color_v2 - color_v1);


    return Eigen::Vector3f(final_color[0], final_color[1], final_color[2]);
}


